package com.learn.controller;

import com.github.pagehelper.PageInfo;
import com.learn.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.learn.dto.Role;

import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @PostMapping("/add")
    public ResponseEntity<Role> addRole(@RequestBody Role role) {
        log.info("addRole: {}", role);
        roleService.insert(role);
        return ResponseEntity.ok(role);
    }

    @PostMapping("/update")
    public ResponseEntity<Role> updateRole(@RequestBody Role role) {
        log.info("updateRole: {}", role);
        roleService.update(role);
        return ResponseEntity.ok(role);
    }

    @PostMapping("/delete")
    public ResponseEntity<Integer> deleteRole(Integer id) {
        log.info("deleteRole: {}", id);
        roleService.delete(id);
        return ResponseEntity.ok(id);
    }
    @PostMapping("/deleteBatch")
    public ResponseEntity<Integer> deleteBatch(@RequestBody List<Integer> ids) {
        log.info("deleteBatch: {}", ids);
        roleService.deletes(ids);
        return ResponseEntity.ok(ids.size());
    }

    @PostMapping("/list")
    public ResponseEntity<List<Role>> list() {
        log.info("list");
        return ResponseEntity.ok(roleService.findAll());
    }
    @GetMapping("/page")
    public PageInfo<Role> page(
            @RequestParam(value = "pageNow", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
            String name, LocalDateTime beginTime, LocalDateTime endTime
    ){
        return roleService.getPage(pageNum,pageSize,name,beginTime,endTime);
    }

}
